#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import urllib.request
import urllib.error
import json
import os

url = 'https://static-data.gaokao.cn/www/2.0/special/level/hot.json?a=www.gaokao.cn'

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'origin': 'https://www.gaokao.cn',
    'priority': 'u=1, i',
    'referer': 'https://www.gaokao.cn/',
    'sec-ch-ua': '""',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '""',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15'
}

try:
    # 创建请求对象
    req = urllib.request.Request(url, headers=headers)
    
    # 发送请求
    with urllib.request.urlopen(req) as response:
        # 读取响应数据
        response_data = response.read().decode('utf-8')
        
        # 解析JSON数据
        data = json.loads(response_data)
        
        # 打印格式化的JSON数据
        print(json.dumps(data, ensure_ascii=False, indent=2))
        
        # 将数据写入txt文件
        with open('热门专业数据.txt', 'w', encoding='utf-8') as f:
            # 写入时间信息
            f.write(f"获取时间: {data['time']}\n\n")
            
            # 写入本科专业信息
            f.write("==== 本科热门专业 ====\n\n")
            for major in data['data']['1']:
                f.write(f"专业名称: {major['name']}\n")
                f.write(f"专业代码: {major['code']}\n")
                f.write(f"所授学位: {major['degree']}\n")
                f.write(f"学制年限: {major['limit_year']}\n")
                f.write(f"男生比例: {major['boy_rate']}%\n")
                f.write(f"女生比例: {major['girl_rate']}%\n")
                f.write(f"平均薪资: {major['salaryavg']}\n")
                f.write(f"五年后平均薪资: {major['fivesalaryavg']}\n")
                f.write("-" * 50 + "\n")
            
            # 写入专科专业信息
            f.write("\n==== 专科热门专业 ====\n\n")
            for major in data['data']['2']:
                f.write(f"专业名称: {major['name']}\n")
                f.write(f"专业代码: {major['code']}\n")
                f.write(f"学制年限: {major['limit_year']}\n")
                f.write(f"男生比例: {major['boy_rate']}%\n")
                f.write(f"女生比例: {major['girl_rate']}%\n")
                f.write(f"平均薪资: {major['salaryavg']}\n")
                f.write(f"五年后平均薪资: {major['fivesalaryavg']}\n")
                f.write("-" * 50 + "\n")
            
            # 写入应用型本科专业信息
            f.write("\n==== 应用型本科热门专业 ====\n\n")
            for major in data['data']['230']:
                f.write(f"专业名称: {major['name']}\n")
                f.write(f"专业代码: {major['code']}\n")
                f.write(f"所授学位: {major['degree']}\n")
                f.write(f"学制年限: {major['limit_year']}\n")
                f.write(f"男生比例: {major['boy_rate']}%\n")
                f.write(f"女生比例: {major['girl_rate']}%\n")
                f.write(f"平均薪资: {major['salaryavg']}\n")
                f.write(f"五年后平均薪资: {major['fivesalaryavg']}\n")
                f.write("-" * 50 + "\n")
        
        print(f"\n数据已成功写入到 '热门专业数据.txt' 文件中。")
        
except urllib.error.URLError as e:
    print(f"请求错误: {e}")
except json.JSONDecodeError:
    print("JSON解析错误")
